﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 精灵类型
/// </summary>
public enum SpriteTypeEnum
{
	Hero,									//玩家
	Monster,								//怪物
	Boss,									//Boss
}

/// <summary>
/// 攻击类型
/// </summary>
public enum AttackTypeEnum
{
	Line,									//直线
}

/// <summary>
/// 移动类型
/// </summary>
public enum MoveTypsEnum
{
	Idle,									//空闲状态, 玩家的移动状态
	Y_Axis_Line,							//y 轴直线运动
	Line_Select_Target,						//y 轴选定目标直线运动
	Y_Axis_Line_X_Axis_PingPong,			//y 轴直线运动, x轴 pingpong
	Y_Axis_Stop_Pos,						//y 轴停留在相对位置
	Y_Axis_Stop_X_Axis_PingPong,			//y 轴停留在相对位置, x轴 pingpong
}

/// <summary>
/// 精灵状态
/// </summary>
public enum SpriteStateEnum
{
	Idle,									//空闲状态
	Move,									//移动状态
	Attack,									//攻击状态
	Defense,								//防御状态
	Die,									//死亡状态
}

/// <summary>
/// 精灵类
/// </summary>
public class TSprite : MonoBehaviour {

	[HideInInspector][SerializeField] int hp;							//血
	[HideInInspector][SerializeField] int mp;							//蓝
	[HideInInspector][SerializeField] int attack;						//攻击
	[HideInInspector][SerializeField] int defense;						//防御
	[HideInInspector][SerializeField] float attackFrequency;			//攻击频率
	[HideInInspector][SerializeField] float attackDistance;			//攻击距离
	[HideInInspector][SerializeField] float speedX;					//移动速度 x轴
	[HideInInspector][SerializeField] float speedY;					//移动速度 y轴
	[HideInInspector][SerializeField] int xPingPongLength;				//x 轴pingpong范围			
	[HideInInspector][SerializeField] int yStopPos;					//y 轴停止位置
	
	[HideInInspector][SerializeField] MoveTypsEnum moveType = MoveTypsEnum.Y_Axis_Line;	//精灵的运动状态
	[HideInInspector][SerializeField] AttackTypeEnum attackType = AttackTypeEnum.Line;		//攻击类型
	[HideInInspector][SerializeField] int bulletId;						//精灵使用的子弹id
	
	SpriteStateEnum spriteState = SpriteStateEnum.Idle;	//当前精灵状态
	
	ViewRect viewRect;						//视野范围对象
	
	Transform target;						//选定的攻击目标
	Vector3 moveVector;
	Vector3 initVector;						//精灵初始位置
	
	float pingPongTime;						//pingpong阶段时间设置
	
	/// <summary>
	/// 初始化函数
	/// </summary>
	public virtual void Init()
	{
		viewRect = ViewRect.GetInstance();
		target = Thero.GetInstance().transform;
		initVector = transform.localPosition;
	}
	
	void Start()
	{
		Init();
	}
	
	void Update()
	{
		OnUpdate();
	}
	
	/// <summary>
	/// 更新函数
	/// </summary>
	public virtual void OnUpdate()
	{
		SpriteMove();
	}
	
	/// <summary>
	/// 死亡函数
	/// </summary>
	public virtual void OnDeath()
	{
		
	}
	
	/// <summary>
	/// 复活函数
	/// </summary>
	public virtual void Relive()
	{
		
	}
	
	/// <summary>
	/// 攻击函数
	/// </summary>
	public virtual void Attack()
	{
		
	}
	
	/// <summary>
	/// 精灵移动方法
	/// </summary>
	public void SpriteMove()
	{
		switch(moveType)
		{
		case MoveTypsEnum.Idle:
			break;
		case MoveTypsEnum.Y_Axis_Line:
			YAxisLine();
			break;
		case MoveTypsEnum.Line_Select_Target:
			LineSelectedTarget();
			break;
		case MoveTypsEnum.Y_Axis_Line_X_Axis_PingPong:
			YAxisLineXAxisPingPong();
			break;
		case MoveTypsEnum.Y_Axis_Stop_Pos:
			YAxisStopPos();
			break;
		case MoveTypsEnum.Y_Axis_Stop_X_Axis_PingPong:
			YAxisStopPosPingPong();
			break;
		default:
			YAxisLine();
			break;
		}
		
		if(!viewRect.IsInViewRect(moveVector))
		{
			Destroy(gameObject);
		}
	}
	
	/// <summary>
	/// y 轴直线移动
	/// </summary>
	void YAxisLine()
	{
		moveVector = transform.localPosition;
		moveVector.y -= SpeedY;
		
		transform.localPosition += moveVector;
	}
	
	/// <summary>
	/// 选定目标, 移动
	/// </summary>
	void LineSelectedTarget()
	{
		if(target == null || transform.localPosition.y <= target.localPosition.y)	//如果精灵的位置在玩家位置后面则改变精灵的位置为 y轴直线运动
		{
			moveType = MoveTypsEnum.Y_Axis_Line;		
		}
		
		float angle = Vector2.Angle(transform.localPosition, target.localPosition) * Mathf.Deg2Rad;
//		moveVector.x += Mathf.Cos(angle) * SpeedY;
//		moveVector.y -= Mathf.Sin(angle) * SpeedY;
		moveVector.x += Mathf.Sin(angle) * SpeedY;
		moveVector.y -= Mathf.Cos(angle) * SpeedY;
		
		transform.localPosition += moveVector;
	}
	
	/// <summary>
	/// Y 轴直线运动, X轴pingpong 运动
	/// </summary>
	void YAxisLineXAxisPingPong()
	{
		pingPongTime += Time.deltaTime;
		moveVector.x = Mathf.PingPong(pingPongTime * SpeedX, XPingPongLength) + initVector.x;
		moveVector.y = transform.localPosition.y - SpeedY;
		
		transform.localPosition = moveVector;
	}
	
	/// <summary>
	/// y 轴直线运动到固定位置, 然后停止
	/// </summary>
	void YAxisStopPos()
	{
		if(transform.localPosition.y <= YStopPos)
			return;
		
		moveVector = transform.localPosition;
		moveVector.y -= SpeedY;
		
		transform.localPosition = moveVector;
	}
	
	/// <summary>
	/// y 轴直线运动到固定位置, 然后X轴 pingpong
	/// </summary>
	void YAxisStopPosPingPong()
	{
		if(transform.localPosition.y <= YStopPos)
		{
			pingPongTime += Time.deltaTime;
			moveVector.x = Mathf.PingPong(pingPongTime * SpeedX, XPingPongLength) + initVector.x;
			moveVector.y = transform.localPosition.y;
		}
		else
		{
			moveVector = transform.localPosition;
			moveVector.y -= SpeedY;
		}
		
		transform.localPosition = moveVector;
	}
	
	public int Hp
	{
		get{return hp;}
		set{hp = value;}
	}
	
	public int Mp
	{
		get{return mp;}
		set{mp = value;}
	}
	
	public int AttackValue
	{
		get{return attack;}
		set{attack = value;}
	}
	
	public int DefenseValue
	{
		get{return defense;}
		set{defense = value;}
	}
	
	public float AttackFrequency
	{
		get{return attackFrequency;}
		set{attackFrequency = value;}
	}
	
	public float AttackDistance
	{
		get{return attackDistance;}
		set{attackDistance = value;}
	}
	
	public float SpeedX
	{
		get{return speedX;}
		set{speedX = value;}
	}
	
	public float SpeedY
	{
		get{return speedY;}
		set{speedY = value;}
	}
	
	public int XPingPongLength
	{
		get{return xPingPongLength;}
		set{xPingPongLength = value;}
	}
	
	public int YStopPos
	{
		get{return yStopPos;}
		set{yStopPos = value;}
	}
	
	public int BulletId
	{
		get{return bulletId;}
		set{bulletId = value;}
	}
	
	public AttackTypeEnum AttackType
	{
		get{return attackType;}
		set{attackType = value;}
	}
	
	public MoveTypsEnum MoveType
	{
		get{return moveType;}
		set{moveType = value;}
	}
	
	public SpriteStateEnum CurrSpriteState
	{
		get{return spriteState;}
		set{spriteState = value;}
	}
}
